package tenant

import (
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/gogf/gf/v2/util/gconv"
	"ruoyi-vue-pro-go/api/common"
	v1 "ruoyi-vue-pro-go/api/tenantpackage/v1"
	"ruoyi-vue-pro-go/internal/dao"
	"ruoyi-vue-pro-go/internal/model/do"
	"ruoyi-vue-pro-go/internal/model/entity"
	"ruoyi-vue-pro-go/internal/service"
)

type (
	sTenantPackage struct{}
)

func init() {
	service.RegisterTenantPackage(New())
}

func New() service.ITenantPackage {
	return &sTenantPackage{}
}

func (s *sTenantPackage) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
	res = &v1.GetListRes{}
	err = dao.SystemTenantPackage.Ctx(ctx).Scan(&res.List)
	return
}

func (s *sTenantPackage) GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error) {
	res = &v1.GetOneRes{}
	err = dao.SystemTenantPackage.Ctx(ctx).Where(do.SystemTenantPackage{
		Id: req.Id,
	}).Scan(&res.SystemTenantPackageVO)
	return
}

func (s *sTenantPackage) GetPage(ctx context.Context, req *v1.GetPageReq) (res *v1.GetPageRes, err error) {
	res = &v1.GetPageRes{
		List:          make([]*entity.SystemTenantPackage, 0),
		CommonPageRes: &common.CommonPageRes{},
	}
	model := dao.SystemTenantPackage.Ctx(ctx)
	if req.Name != "" {
		model = model.WhereLike("name", "%"+req.Name+"%")
	}
	if req.Status != -1 {
		model = model.Where("status", req.Status)
	}
	// 添加时间范围条件
	if len(req.CreateTime) != 0 {
		beginTime := gtime.New(req.CreateTime[0])
		endTime := gtime.New(req.CreateTime[1])
		if beginTime != nil && endTime != nil {
			// 验证时间顺序
			if beginTime.Time.After(endTime.Time) {
				return nil, gerror.New("开始时间不能晚于结束时间")
			}

			model = model.WhereBetween("create_time", beginTime, endTime)
		} else if beginTime != nil {
			model = model.WhereGTE("create_time", beginTime)
		} else if endTime != nil {
			model = model.WhereLTE("create_time", endTime)
		}
	}
	model = model.Where(do.SystemTenantPackage{
		Deleted: false,
	})
	if req.PageSize != 0 {
		res.PageNo = req.PageNo
		res.PageSize = req.PageSize
		model = model.Page(req.PageNo, req.PageSize)
	}
	err = model.ScanAndCount(&res.List, &res.Total, false)
	return
}

func (s *sTenantPackage) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
	res = &v1.UpdateRes{}
	_, err = dao.SystemTenantPackage.Ctx(ctx).Data(req).Where(do.SystemTenantPackage{
		Id: req.Id,
	}).Update()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "tenant package update failed")
	}
	return
}

func (s *sTenantPackage) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
	res = &v1.CreateRes{}
	var cls = dao.SystemTenantPackage.Columns()
	count, err := dao.SystemTenantPackage.Ctx(ctx).
		Where(cls.Name, req.Name).Count()
	if err != nil {
		return res, err
	}
	if count > 0 {
		return res, gerror.NewCode(gcode.CodeDbOperationError, "租户套餐已存在")
	}
	_, err = dao.SystemTenantPackage.Ctx(ctx).Data(req).Insert()
	if err != nil {
		return res, gerror.NewCode(gcode.CodeDbOperationError, "tenant package create failed")
	}
	return
}

func (s *sTenantPackage) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
	res = &v1.DeleteRes{}
	var cls = dao.SystemTenantPackage.Columns()
	_, err = dao.SystemTenantPackage.Ctx(ctx).Where(cls.Id, req.Id).Delete()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "tenant package delete failed")
	}
	return
}

func (s *sTenantPackage) DeleteList(ctx context.Context, req *v1.DeleteListReq) (res *v1.DeleteListRes, err error) {
	res = &v1.DeleteListRes{}
	var cls = dao.SystemTenantPackage.Columns()
	// 参数转换
	ids := gconv.Ints(gstr.Split(req.Ids, ","))
	if len(ids) == 0 {
		return nil, gerror.New("无效的ID参数")
	}
	_, err = dao.SystemTenantPackage.Ctx(ctx).WhereIn(cls.Id, ids).Delete()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "tenant package delete list failed")
	}
	return
}
